package com.liankuan.console.util;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
import java.util.Random;
public class DES {
    private static final Random RANDOM = new Random();

    public static String getKey() {
        StringBuilder sb = new StringBuilder();
        int i = 128;
        while (i-->0) sb.append(RANDOM.nextInt(10));
        return sb.toString();
    }

    /**
     * md5加密算法
     * @param str
     * @return
     */
    public static String getMD5String(String str) {
        if (str == null || str.trim().length() == 0) {
            return "";
        }
        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            re_md5 = buf.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return re_md5;
    }

    /**
     * 加密
     * @param clear String 明文
     * @param key String 秘钥
     * @return String
     */
    public static String encrypt(String clear, String key) {
        try{
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(key.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return Base64.getEncoder().encodeToString(cipher.doFinal(clear.getBytes()));
        }catch(Throwable e){
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 解密
     * @param cipher String
     * @param key String
     * @return String
     */
    public static String decrypt(String cipher, String key)  {

        try {
            // DES算法要求有一个可信任的随机数源
            SecureRandom random = new SecureRandom();
            // 创建一个DESKeySpec对象
            DESKeySpec desKey = null;
            desKey = new DESKeySpec(key.getBytes());
            // 创建一个密匙工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // 将DESKeySpec对象转换成SecretKey对象
            SecretKey secureKey = keyFactory.generateSecret(desKey);
            // Cipher对象实际完成解密操作
            Cipher c = Cipher.getInstance("DES");
            // 用密匙初始化Cipher对象
            c.init(Cipher.DECRYPT_MODE, secureKey, random);
            // 真正开始解密操作
            return new String(c.doFinal(Base64.getDecoder().decode(cipher)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
